﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class Admin_Default2 : System.Web.UI.Page
{
    clsDBOperation objDBOp = new clsDBOperation();
    Hashtable hstParameters = new Hashtable();
    DataSet dsadmin = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["dsGolbal"] == null)
        {
            Response.Redirect("../");
        }
        dsadmin = (DataSet)Session.Contents["dsGolbal"];

        if (!IsPostBack)
        {
            GetCategory();
        }

        btnAddCategory.Attributes.Add("onclick", " return ValidateAddCategory(" + txtAddCategory.ClientID + ")");
    }

    private void GetCategory()
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_Category");
            dssave = (DataSet)objDBOp.GetDataset("sp_Category", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {
                grdCategory.DataSource = dssave;
                grdCategory.DataBind();

            }
            else
            {
                grdCategory.DataSource = null;
                grdCategory.DataBind();
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }


    protected void btnAddCategory_Click(object sender, EventArgs e)
    {

        Save();
        GetCategory();


    }

    public void Save()
    {
        try
        {
            int IsSaved;
            hstParameters.Clear();
            hstParameters.Add("Action", "Insert_Category");
            hstParameters.Add("adminid", dsadmin.Tables[0].Rows[0][1].ToString());
            hstParameters.Add("categoryname", txtAddCategory.Text.Trim());

            IsSaved = Convert.ToInt32(objDBOp.ExecuteNonQuery("sp_Category ", CommandType.StoredProcedure, hstParameters));
            if (IsSaved > 0)
                lblmsg.Text = "Record Saved Successfully";
            txtAddCategory.Text = "";
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }


    protected void grdCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdCategory.EditIndex = -1;
        GetCategory();
    }

    protected void grdCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdCategory.EditIndex = e.NewEditIndex;
        GetCategory();
        TextBox txtCategoryName = grdCategory.Rows[e.NewEditIndex].FindControl("txtCategoryName") as TextBox;
        LinkButton edit = grdCategory.Rows[e.NewEditIndex].Cells[2].Controls[0] as LinkButton;
        edit.Attributes.Add("onclick", " return ValidateAddCategory(" + txtCategoryName.ClientID + ")");

    }

    protected void grdCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int Categoryid = Convert.ToInt32(grdCategory.DataKeys[e.RowIndex].Value);
        TextBox txtCategoryName = grdCategory.Rows[e.RowIndex].FindControl("txtCategoryName") as TextBox;
        if (txtCategoryName != null)
            UpdateCategory(Categoryid, txtCategoryName.Text.Trim());
        grdCategory.EditIndex = -1;
        GetCategory();
    }

    private void UpdateCategory(int CategoryId, string CategoryName)
    {
        try
        {
            hstParameters.Clear();
            hstParameters.Add("Action", "Update_Category");
            hstParameters.Add("categoryId", CategoryId);
            hstParameters.Add("categoryname", CategoryName);
            hstParameters.Add("adminid", dsadmin.Tables[0].Rows[0][1].ToString());
            int intVal = Convert.ToInt32(objDBOp.ExecuteNonQuery("sp_Category", CommandType.StoredProcedure, hstParameters));
            if (intVal == 1)
                lblmsg.Text = "Category Update Successfully!!!";

        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }


}
